package cn.wzg.server.Servers.Serverimpl;

import cn.wzg.server.Servers.RPCServer;
import cn.wzg.server.Workers.WorkThread;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class BIORPCServer implements RPCServer {
    Map<String,Object> serviceprovider;
    private final ThreadPoolExecutor threadPool;

    public BIORPCServer(Map<String, Object> serviceprovider) {
        this.serviceprovider = serviceprovider;
        this.threadPool = new ThreadPoolExecutor(Runtime.getRuntime().availableProcessors(),
                1000, 60, TimeUnit.SECONDS, new ArrayBlockingQueue<>(100));;
    }

    @Override
    public void start(int port) throws IOException {
        ServerSocket socket = new ServerSocket(port);
        while (true) {
            Socket accept = socket.accept();
            System.out.println("服务端启动");
            //使用线程池接受任务
            threadPool.execute(new WorkThread(accept,serviceprovider));
        }
    }

    @Override
    public void stop() {

    }
}
